package com.banban.user.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.banban.user.domain.SysDictDetailEntity;
import com.banban.user.dto.DeleteDTO;
import com.banban.user.exception.BusinessException;
import com.banban.user.service.SysDictDetailService;
import com.banban.user.vo.DataResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "字典明细管理")
@RestController
@RequestMapping("/sysDictDetail")
public class SysDictDetailController {
    @Resource
    private SysDictDetailService sysDictDetailService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public DataResult add(@RequestBody @Valid SysDictDetailEntity sysDictDetail) {
        LambdaQueryWrapper<SysDictDetailEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysDictDetailEntity::getLabel, sysDictDetail.getLabel());
        queryWrapper.eq(SysDictDetailEntity::getDictId, sysDictDetail.getDictId());
        SysDictDetailEntity q = sysDictDetailService.getOne(queryWrapper);
        if (q != null){
            throw new BusinessException("字典名称-字典值已存在");
        }
        sysDictDetailService.save(sysDictDetail);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public DataResult delete(@RequestBody DeleteDTO dto) {
        sysDictDetailService.removeByIds(dto.getIds());
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public DataResult update(@RequestBody @Valid SysDictDetailEntity sysDictDetail) {
        if (sysDictDetail.getId() == null){
            throw new BusinessException("字典明细ID不能为空");
        }
        LambdaQueryWrapper<SysDictDetailEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysDictDetailEntity::getValue, sysDictDetail.getValue());
        queryWrapper.eq(SysDictDetailEntity::getDictId, sysDictDetail.getDictId());
        SysDictDetailEntity q = sysDictDetailService.getOne(queryWrapper);
        if (q != null && !q.getId().equals(sysDictDetail.getId())){
            throw new BusinessException("字典名称-字典值已存在");
        }
        sysDictDetailService.updateById(sysDictDetail);
        return DataResult.success();
    }


    @ApiOperation(value = "查询列表数据")
    @PostMapping("/listByPage")
    public DataResult findListByPage(@RequestBody SysDictDetailEntity sysDictDetail) {
        if (StringUtils.isEmpty(sysDictDetail.getDictId())) {
            return DataResult.success(new Page<>());
        }
        return DataResult.success(sysDictDetailService.listByPage(sysDictDetail.getQueryPage(), sysDictDetail.getDictId()));
    }

}
